// miniprogram/pages/mine/index/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    favorite: false,
    term: encodeURIComponent('http://mp.weixin.qq.com/s?__biz=Mzg3MDU1NDk0NA==&mid=100000067&idx=1&sn=67303185b67ad327b5b4834c1325e207&chksm=4e8d4d6c79fac47abb7608269cc4a41c5ca62b1fce3c61c0ab83d2878e3e7e1b7214f692b75f#rd')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.login(user => {
      this.setData({
        user: user
      });

    });
  },

  getUserInfo: function (e) {
    console.log(e)
    let user = this.data.user;
    if (!user) user = {};
    wx.getUserProfile({
      desc: '业务需要',
      success: res => {
        console.log(res)
        let userinfo = res.userInfo
        //

        if (!userinfo) {
          this.setData({
            modalname: 'shouquan'
          })

        } else {
          this.hideModal()
          user.nickName = userinfo.nickName;
          user.avatarUrl = userinfo.avatarUrl;
          user.country = userinfo.country;
          user.province = userinfo.province;
          user.gender = userinfo.gender;
          user.city = userinfo.city;


          if (!user._id) {
            user._id = user.openid;
            wx.cloud.callFunction({
              name: 'crud',
              data: {
                table: 'user',
                op: 'add',
                data: user,
              },
              success: res => {
                this.onLoad()

              }
            })
          } else {
            user._id = user.openid;
            wx.cloud.callFunction({
              name: 'crud',
              data: {
                table: 'user',
                op: 'update',
                data: user
              },

            })
          }
        }
      }
    })


  },
  toggleFavorite() {
    this.setData({
      favorite: this.data.favorite ? false : true
    })
  },
  clearStorage() {
    wx.clearStorage({
      success: (res) => {
        wx.showToast({
          title: '清除完成',
        })
      },
    })
  },
  downloadFile: function (e) {
    console.log(e);
    let type = e.currentTarget.dataset.type;
    let url = e.currentTarget.dataset.url;
    switch (type) {
      case "pdf":
        url += 'pdf';
        break;
      case "word":
        url += 'docx';
        break;
      case "excel":
        url += 'xlsx';
        break;
      default:
        url += 'pptx';
        break;
    }
    wx.showLoading({

      title: '正在打开...',
    })
    wx.downloadFile({
      url: url,
      header: {},
      success: function (res) {
        var filePath = res.tempFilePath;
        console.log(filePath);
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            console.log('打开文档成功')
            wx.hideLoading()
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        })
      },
      fail: function (res) {
        console.log('文件下载失败');
      },
      complete: function (res) {},
    })
  },
  hideModal() {
    this.setData({
      modalname: null
    });
  },
  clip(e){
    let content=e.currentTarget.dataset.content
    wx.setClipboardData({
      data: content,
      success (res) {
      //  wx.hideToast(); //隐藏复制成功的弹窗提示,根据需求可选
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})